import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:kegelutils/controller_main.dart';
import 'package:kegelutils/dailogs.dart';
import 'package:kegelutils/state_player.dart';
import 'package:kegelutils/utils/utils_time.dart';

class PlayInfoView extends StatelessWidget {
  final int index;

  const PlayInfoView(this.index, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          const SizedBox(height: 5.0),
          buildBottomButton(),
          const SizedBox(height: 10.0),
          buildRemainTimeRow(),
        ],
      ),
    );
  }

  Widget buildRemainTimeRow() {
    return Obx(() {
      int curRunningIndex = MainController.to.curRunningIndex.value;
      int durationSeconds = MainController.to.duration.value.inSeconds;
      int positionSeconds = MainController.to.position.value.inSeconds;
      PlayState state = MainController.to.state.value;
      String remainTimeStr = "--";
      if (curRunningIndex == index) {
        remainTimeStr =
            TimeUtils.formatHHMMFromSecond(durationSeconds - positionSeconds);
      } else {
        remainTimeStr = "--";
      }
      if (state != PlayState.playing) {
        remainTimeStr = "--";
      }
      return Container(
        child: Text(
          '剩余时间：$remainTimeStr',
          style: const TextStyle(
            fontSize: 20.0,
          ),
        ),
      );
    });
  }

  Obx buildBottomButton() {
    return Obx(() {
      PlayState state = MainController.to.state.value;
      int curRunningIndex = MainController.to.curRunningIndex.value;
      String? title;
      if (curRunningIndex == index) {
        switch (state) {
          case PlayState.stopped:
          case PlayState.paused:
          case PlayState.completed:
            title = '开始锻炼';
            break;
          case PlayState.playing:
            title = '终止锻炼';
        }
      } else {
        title = '开始锻炼';
      }
      return GestureDetector(
        onTap: () {
          if (curRunningIndex == index) {
            switch (state) {
              case PlayState.stopped:
              case PlayState.paused:
              case PlayState.completed:
                MainController.to.play();
                break;
              case PlayState.playing:
                MainController.to.stop();
            }
          } else {
            //todo 提示切换
            popChangePlanDialog(curRunningIndex);
          }
        },
        child: Container(
          child: Text(
            title,
            style: TextStyle(
              fontSize: 25.0,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      );
    });
  }
}
